package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.controller.from.MovieScoreFrom;
import com.demo.controller.from.MovieScoreListFrom;
import com.demo.exetion.MainExection;
import com.demo.model.MovieScore;
import com.demo.dao.MovieScoreDao;
import com.demo.service.MovieScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2022-04-13
 */
@Service
public class MovieScoreServiceImpl extends ServiceImpl<MovieScoreDao, MovieScore> implements MovieScoreService {
    @Autowired
    private RedisTemplate<String, Object> ort;

    @Override
    public Boolean addScore(MovieScoreListFrom movieScoreListFrom) throws MainExection {
        List<MovieScoreFrom> movieScoreFromList = movieScoreListFrom.getMovieScoreFromList();
        if (movieScoreFromList==null) throw new MainExection(500,"参数列表不能为空");
        //将未评论的票务评分
        List<MovieScore> movieScoreFromListNew = new ArrayList<>();
        for (MovieScoreFrom movieScoreFrom : movieScoreFromList) {
            Object o = ort.opsForValue().get("score:Ticketing_id" + movieScoreFrom.getTicketingId());
            if (o!=null){
                MovieScore movieScore = CommonUtil.copyOne(movieScoreFrom, MovieScore.class);
                movieScoreFromListNew.add(movieScore);
                ort.opsForValue().set("score:Ticketing_id" + movieScoreFrom.getTicketingId(),""+movieScoreFrom.getScore());
            }else{
                throw new MainExection(500,"已过评论时间");
            }
        }
//        评分录入数据库
        boolean b = this.saveBatch(movieScoreFromListNew);
        if (!b)throw new MainExection(500,"新增评分错误");
        return true;
    }
}
